From 15c73a2b1aae5be740efa19b3d20f9e6a26f8970 Mon Sep 17 00:00:00 2001 From: Arc Riley Date: Tue, 30 Jun 2015 10:57:49 -0700 Subject: [PATCH] Add gtk_widget_set_font_options and gtk_widget_get_font_options This allows a widget to override global font_options, such as hinting and subpixel order. The widget's PangoContext is updated when this is set. Some update code from gtk_widget_update_pango_context was moved to update_pango_context so that gtk_widget_update_pango_context runs it. http://bugzilla.gnome.org/show_bug.cgi?id=751677 --- docs/reference/gtk/gtk3-sections.txt | 2 + gtk/gtkwidget.c | 74 ++++++++++++++++++++++++---- gtk/gtkwidget.h | 5 ++ 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt index 1967110df9..5df974531c 100644 --- a/docs/reference/gtk/gtk3-sections.txt +++ b/docs/reference/gtk/gtk3-sections.txt @@ -5470,6 +5470,8 @@ gtk_widget_modify_font gtk_widget_modify_cursor gtk_widget_create_pango_context gtk_widget_get_pango_context +gtk_widget_set_font_options +gtk_widget_get_font_options gtk_widget_create_pango_layout gtk_widget_render_icon gtk_widget_render_icon_pixbuf diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c index d9230f5ac3..c1fd68a3f3 100644 --- a/gtk/gtkwidget.c +++ b/gtk/gtkwidget.c @@ -599,6 +599,8 @@ struct _GtkWidgetPrivate #endif /* G_ENABLE_DEBUG */ GList *event_controllers; + + cairo_font_options_t *font_options; }; struct _GtkWidgetClassPrivate @@ -10314,10 +10316,11 @@ gtk_widget_get_pango_context (GtkWidget *widget) static void update_pango_context (GtkWidget *widget, - PangoContext *context) + PangoContext *context) { PangoFontDescription *font_desc; GtkStyleContext *style_context; + GdkScreen *screen; style_context = gtk_widget_get_style_context (widget); gtk_style_context_get (style_context, @@ -10337,6 +10340,18 @@ update_pango_context (GtkWidget *widget, _gtk_style_context_peek_property (style_context, GTK_CSS_PROPERTY_DPI), 100)); + + screen = gtk_widget_get_screen_unchecked (widget); + if (widget->priv->font_options) + { + pango_cairo_context_set_font_options (context, + widget->priv->font_options); + } + else if (screen) + { + pango_cairo_context_set_font_options (context, + gdk_screen_get_font_options (screen)); + } } static void @@ -10345,20 +10360,58 @@ gtk_widget_update_pango_context (GtkWidget *widget) PangoContext *context = gtk_widget_peek_pango_context (widget); if (context) + update_pango_context (widget, context); +} + +/** + * gtk_widget_set_font_options: + * @widget: a #GtkWidget + * @options: (allow-none): a #cairo_font_options_t, or %NULL to unset any + * previously set default font options. + * + * Sets the #cairo_font_options_t used for Pango rendering in this widget. + * When not set, the default font options for the #GdkScreen will be used. + * + * Since: 3.18 + **/ +void +gtk_widget_set_font_options (GtkWidget *widget, + const cairo_font_options_t *options) +{ + g_return_if_fail (GTK_IS_WIDGET (widget)); + + if (widget->priv->font_options != options) { - GdkScreen *screen; + if (widget->priv->font_options) + cairo_font_options_destroy (widget->priv->font_options); - update_pango_context (widget, context); + if (options) + widget->priv->font_options = cairo_font_options_copy (options); + else + widget->priv->font_options = NULL; - screen = gtk_widget_get_screen_unchecked (widget); - if (screen) - { - pango_cairo_context_set_font_options (context, - gdk_screen_get_font_options (screen)); - } + gtk_widget_update_pango_context (widget); } } +/** + * gtk_widget_get_font_options: + * @widget: a #GtkWidget + * + * Returns the #cairo_font_options_t used for Pango rendering. When not set, + * the defaults font options for the #GdkScreen will be used. + * + * Returns: (transfer none): the #cairo_font_options_t or %NULL if not set + * + * Since: 3.18 + **/ +const cairo_font_options_t * +gtk_widget_get_font_options (GtkWidget *widget) +{ + g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL); + return widget->priv->font_options; +} + /** * gtk_widget_create_pango_context: * @widget: a #GtkWidget @@ -12214,6 +12267,9 @@ gtk_widget_finalize (GObject *object) if (priv->context) g_object_unref (priv->context); + if (widget->priv->font_options) + cairo_font_options_destroy (widget->priv->font_options); + _gtk_size_request_cache_free (&priv->requests); for (l = priv->event_controllers; l; l = l->next) diff --git a/gtk/gtkwidget.h b/gtk/gtkwidget.h index 450cd1968e..86f8e89d53 100644 --- a/gtk/gtkwidget.h +++ b/gtk/gtkwidget.h @@ -1178,6 +1178,11 @@ GDK_AVAILABLE_IN_ALL PangoContext *gtk_widget_create_pango_context (GtkWidget *widget); GDK_AVAILABLE_IN_ALL PangoContext *gtk_widget_get_pango_context (GtkWidget *widget); +GDK_AVAILABLE_IN_3_18 +void gtk_widget_set_font_options (GtkWidget *widget, + const cairo_font_options_t *options); +GDK_AVAILABLE_IN_3_18 +const cairo_font_options_t *gtk_widget_get_font_options (GtkWidget *widget); GDK_AVAILABLE_IN_ALL PangoLayout *gtk_widget_create_pango_layout (GtkWidget *widget, const gchar *text); -- 2.30.2